package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PersGoSmartOrderQuantityAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.goods.GoodsHelper;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * @author zhouJiao
 * @create 2022-1-5-18:30
 */
@Service
@AttrHandler(PersGoSmartOrderQuantityAttrHandler.class)
public class PersGoSmartOrderQuantityAttrLinkHandler extends AbstractAttrLinkageHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    private static final String ORDER_QUANTITY_ATTR = "200001696";
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 不是框架的主产品不处理
        KiteOfferInst mainOfferInst = querySceneInstService.getGiftOfferInst(data.getSceneInstId());
        if (Objects.isNull(mainOfferInst)) {
            return;
        }
        if (!GoodsHelper.isGift(mainOfferInst.getOfferId())) {
            return;
        }
        // 框架主产品的订购数量
        KiteAttr orderQuantityAttr = inst.getAttr(ORDER_QUANTITY_ATTR);
        if (Objects.nonNull(orderQuantityAttr)) {
            // 获取礼包下的所有主套餐
            List<KiteOfferInst> kiteOfferInstList = querySceneInstService.getSceneInstDTO(data.getSceneInstId()).getMainOfferInsts();
//            List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInstsFromGiftOfferInst(data.getCustId(), mainOfferInst.getOfferInstId());
            if (KiteListUtils.isNotEmpty(kiteOfferInstList)) {
                kiteOfferInstList.forEach(kiteOfferInst -> {
                    // 获取该主套餐下的所有接入产品
                    List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(data.getCustId(), kiteOfferInst.getOfferInstId());
                    if (KiteListUtils.isNotEmpty(prodInstList)) {
                        prodInstList.forEach(kiteProdInst -> {
                            KiteAttr subOrderQuantityAttr = kiteProdInst.getAttr(ORDER_QUANTITY_ATTR);
                            if (Objects.nonNull(subOrderQuantityAttr)) {
                                subOrderQuantityAttr.setValue(orderQuantityAttr.getValue());
                                subOrderQuantityAttr.setValueDesc(orderQuantityAttr.getValueDesc());
                                operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), subOrderQuantityAttr);
                            }
                        });
                    }
                });
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
